package com.yoso.shop.application.service.impl;

import com.yoso.base.service.BaseService;
import com.yoso.shop.application.service.ICommodityService;
import com.yoso.shop.application.service.IDictionaryService;
import com.yoso.shop.domain.dictionary.DictionaryEntity;
import com.yoso.shop.domain.dictionary.DictionaryEntityExample;
import com.yoso.shop.infrastructure.constants.commodity.DictionaryGroupEnums;
import com.yoso.shop.interfaces.vo.commodity.CommodityVo;
import com.yoso.shop.interfaces.vo.dictionary.DictionaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yoso
 * @date 2022/2/20
 */
@Service
public class DictionaryServiceImpl extends BaseService<DictionaryEntity, DictionaryVo, DictionaryEntityExample> implements IDictionaryService {

    @Autowired
    private ICommodityService commodityService;

    @Override
    public DictionaryEntityExample getEntityExample(Map<String, String> paramMap) {
        return null;
    }

    @Override
    public DictionaryEntity V2E(DictionaryVo data) {
        return this.convert(data, DictionaryEntity.class);
    }

    @Override
    public DictionaryVo E2V(DictionaryEntity data) {
        return this.convert(data, DictionaryVo.class);
    }

    /**
     * 获取目标分组数据
     */
    @Override
    public List<DictionaryVo> getDataByGroup(String group) {
        DictionaryEntityExample example = new DictionaryEntityExample();
        example.createCriteria()
                .andTypeEqualTo(group);
        return this.selectByExample(example);
    }

    /**
     * 获取目标分组数据
     */
    @Override
    public List<DictionaryVo> getDataByGroup(List<String> groupList) {
        DictionaryEntityExample example = new DictionaryEntityExample();
        example.createCriteria()
                .andTypeIn(groupList);
        return this.selectByExample(example);
    }

    /**
     * 获取轮播图，热词，新闻数据
     */
    @Override
    public Map<String, List<DictionaryVo>> getData() {
        DictionaryEntityExample example = new DictionaryEntityExample();
        example.createCriteria().andTypeIn(Arrays.asList(
                DictionaryGroupEnums.HOT.toString(),
                DictionaryGroupEnums.LUNBO.toString(),
                DictionaryGroupEnums.NEWS.toString()));
        List<DictionaryVo> dictionaryVos = this.selectByExample(example);
        return dictionaryVos.parallelStream().collect(Collectors.groupingBy(DictionaryVo::getType));
    }

    /**
     * 修改商品标签
     */
    @Override
    public int updateCommodityTags(CommodityVo vo) {
        String tags = String.join(",", vo.getTags());
        DictionaryVo dictionaryVo = new DictionaryVo();
        dictionaryVo.setType(DictionaryGroupEnums.COMMODITY_TAG.toString());
        dictionaryVo.setTag(vo.getCode());
        dictionaryVo.setInfo(tags);
        return this.insert(dictionaryVo);
    }
}
